<?php

namespace application\controller\cpservice;

use application\controller\cpservice\ServiceController;
use application\service\ImageService;
use core\system\HttpRequest;

class ImageController extends ServiceController {

    private $imageService;

    public function __construct() {
        parent::__construct();
        $this->imageService = new ImageService();
    }

    /**
     * @mapping(value=cpservice/image/add.json)
     */
    public function add() {
        $url = trim(HttpRequest::POST("url"));
        $image = isset($_FILES['imageFile']) ? $_FILES['imageFile'] : null;
        $resp = '';
        if (isset($url) && $url != '') {
            $resp = $this->imageService->dowload($url, HttpRequest::GET("type"), HttpRequest::GET("target"));
        } else {
            $resp = $this->imageService->upload($image, HttpRequest::GET("type"), HttpRequest::GET("target"));
        }
        $this->view->response($resp);
    }

    /**
     * @mapping(value=cpservice/image/getbytarget.json)
     */
    public function getByTarget() {
        $images = $this->imageService->get(HttpRequest::GET("target"), HttpRequest::GET("type"));
        $this->view->response(true, "Danh sách ảnh", $images);
    }

    /**
     * @mapping(value=cpservice/image/delete.json)
     */
    public function delete() {
        $url = trim(HttpRequest::POST("url"));
        $resp = $this->imageService->delete($url);
        $this->view->response($resp);
    }

}
